package cn.t.converter.device;


import cn.t.constants.CommonConstants;
import cn.t.core.converter.CoreDTOConverter;
import cn.t.dto.device.DeviceOpenRecordDTO;
import cn.t.dto.device.vo.OpenCloseDTO;
import cn.t.enums.user.RoleTypeEnum;
import cn.t.model.common.MailDO;
import cn.t.model.device.DeviceOpenRecordDO;
import cn.t.request.OpenRecordReq;
import cn.t.vo.order.OpenRecordVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

/**
 * 设备开门记录表(DeviceOpenRecord)dto转domain
 *
 * @author t
 * @since 2024-05-24 10:19:34
 */
@Mapper(componentModel = "spring")
public interface DeviceOpenRecordDTOConverter extends CoreDTOConverter<DeviceOpenRecordDTO, DeviceOpenRecordDO> {
        List<DeviceOpenRecordDTO> do2DtoList(List<DeviceOpenRecordDO> deviceOpenRecords);
        @Mapping(target = "gmtOpenStr", expression = "java(cn.t.utils.DateUtil.getDateStr(deviceOpenRecord.getGmtOpen()))")
        @Mapping(target = "userName", expression = "java(deviceOpenRecord.getRecipient())")
        MailDO do2DtoMail(DeviceOpenRecordDO deviceOpenRecord);

        @Mapping(target = "gmtOpen", expression = "java(cn.t.utils.DateUtil.dateByStr(openCloseWeightVO.getRecordAt()))")
        @Mapping(target = "businessType", expression = "java(openCloseWeightVO.getOptionTypeEnum().getCode())")
        @Mapping(target = "recipient", expression = "java(openCloseWeightVO.getUserName())")
        DeviceOpenRecordDO  vo2Do(OpenCloseDTO openCloseWeightVO);
        @Mapping(target = "businessType", expression = "java(transferRoleToBusinessType(request.getRole(), true))")
        DeviceOpenRecordDO req2Do(OpenRecordReq request);

        @Mapping(target = "openAt", expression = "java(cn.t.utils.DateUtil.getDateStr(deviceOpenRecordDO.getGmtOpen()))")
        @Mapping(target = "closeAt", expression = "java(cn.t.utils.DateUtil.getDateStr(deviceOpenRecordDO.getGmtClose()))")
        @Mapping(target = "role", expression = "java(getRole(deviceOpenRecordDO.getBusinessType(), true))")
        @Mapping(target = "roleName", expression = "java(getRoleName(deviceOpenRecordDO.getBusinessType()))")
        OpenRecordVO do2OpenRecordVo(DeviceOpenRecordDO deviceOpenRecordDO);

        default Integer transferRoleToBusinessType(Integer role, boolean transfer) {
                if (RoleTypeEnum.MATERIAL_ISSUE.getCode().equals(role)) {
                        return 1;
                } else if (RoleTypeEnum.STOCK_REPLENISH.getCode().equals(role)) {
                        return 2;
                }
                return null;
        }
//flag 
        default Integer getRole(Integer businessType,boolean flag) {
                if (CommonConstants.ONE.equals(businessType)) {
                        return RoleTypeEnum.MATERIAL_ISSUE.getCode();
                } else {
                        return RoleTypeEnum.STOCK_REPLENISH.getCode();
                }
        }

        default String getRoleName(Integer businessType) {
                if (CommonConstants.ONE.equals(businessType)) {
                        return RoleTypeEnum.MATERIAL_ISSUE.getName();
                } else {
                        return RoleTypeEnum.STOCK_REPLENISH.getName();
                }
        }
}

